<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../../">
  <title data-ice="title">lib/backend/mongodb.js | API Document</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  
  
  <script src="script/manual.js"></script>
</head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./">Home</a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  
  <a data-ice="repoURL" href="https://git.oschina.net/kinuxroot/unique-model.js.git">Repository</a>
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
</header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/session.js~Session.html">Session</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createSession">createSession</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-enablePersist">enablePersist</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isPersistEnabled">isPersistEnabled</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-BaseModel">BaseModel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Types">Types</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-model">model</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">backend</div><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/backend/base.js~Dao.html">Dao</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/backend/base.js~Session.html">Session</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">model</div><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/model/base.js~Model.html">Model</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createModel">createModel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getModel">getModel</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">type</div><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/type/index.js~BaseType.html">BaseType</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/type/index.js~Double.html">Double</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/type/index.js~Integer.html">Integer</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/type/index.js~Text.html">Text</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/type/index.js~UObject.html">UObject</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-defineType">defineType</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-toUniqueModel">toUniqueModel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-allTypes">allTypes</a></span></span></li>
</ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">lib/backend/mongodb.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">&apos;use strict&apos;

module.exports = function() {
    let um = require(&apos;..&apos;);
    let base = require(&apos;./base&apos;);

    let mongoose = require(&apos;mongoose&apos;);

    let typeMapper = {
        Id: String,
        Text: String,
        Integer: Number,
        Double: Number,
        UObject: mongoose.Schema.Types.ObjectId
    }

    function toMongoType(fieldType) {
        return typeMapper[fieldType];
    }

    class Dao extends base.Dao {
        constructor(session, uniqueModel) {
            super(session);

            let modelName = uniqueModel._meta.name;
            let uniqueSchema = uniqueModel._meta.schema;

            this.schema = this.fromUniqueSchema(uniqueSchema);
            this.mongoModel = mongoose.model(modelName, this.schema);
            this.uniqueModel = uniqueModel;
        }

        fromUniqueSchema(uniqueSchema) {
            let schemaDescription = {};
            for ( let fieldName in uniqueSchema ) {
                let fieldType = uniqueSchema[fieldName];
                schemaDescription[fieldName] = toMongoType(fieldType._name);
            }

            return new mongoose.Schema(schemaDescription);
        }

        executeAsync(operation, model, callback) {
            this.mongoModel[operation](model, function(err, result) {
                callback(err, result);
            });
        }

        getObjectIdKey() {
            return &apos;_id&apos;;
        }

        // override
        createAsync(backendModel, callback) {
            this.executeAsync(&apos;create&apos;, backendModel, callback);
        }

        // override
        findAsync(condition, callback) {
            this.executeAsync(&apos;find&apos;, condition, callback);
        }

        // override
        findOneAsync(condition, callback) {
            this.executeAsync(&apos;findOne&apos;, condition, callback);
        }

        // override
        updateAsync(condition, doc, options, callback) {
            if ( !callback ) {
                callback = options;
                this.mongoModel.update(condition, doc, function(err, result) {
                    callback(err, result);
                });
            }
            else {
                this.mongoModel.update(condition, doc, options, function(err, result) {
                    callback(err, result);
                });
            }
        }

        // override
        updateOneAsync(condition, doc, options, callback) {
            if ( !callback ) {
                callback = options;
                this.mongoModel.findOneAndUpdate(condition, doc, function(err, result) {
                    callback(err, result);
                });
            }
            else {
                this.mongoModel.findOneAndUpdate(condition, doc, options, function(err, result) {
                    callback(err, result);
                });
            }
        }

        // override
        removeAsync(condition, callback) {
            this.executeAsync(&apos;remove&apos;, condition, callback);
        }
    }

    class Session extends base.Session {
        constructor(options) {
            super(options);

            let uri = options.uri;
            let mongoOptions = options.options;

            mongoose.connect(uri, mongoOptions);

            this.mongo = mongoose;
            this.db = mongoose.connection;
            this.db.on(&apos;error&apos;, console.error.bind(console, &apos;connection error:&apos;));
        }

        // override
        createDao(model) {
            return new Dao(this, model);
        }
    }

    return {
        Session
    }
}();
</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.4.3)</span></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
